<template>
  <div v-if="visible" class="ppt-viewer-overlay" @click="handleOverlayClick">
    <div class="ppt-viewer-container" @click.stop>
      <!-- 关闭按钮 -->
      <button class="close-btn" @click="closePPT">
        <i class="fas fa-times"></i>
      </button>
      
      <!-- PPT标题 -->
      <div class="ppt-header">
        <h3>{{ courseTitle }} - PPT演示</h3>
        <div class="page-indicator">
          {{ currentPage }} / {{ totalPages }}
        </div>
      </div>
      
      <!-- PPT内容区域 -->
      <div class="ppt-content">
        <div class="ppt-image-container">
          <img 
            :src="currentImageSrc" 
            :alt="`PPT第${currentPage}页`"
            class="ppt-image"
            @load="onImageLoad"
            @error="onImageError"
          />
          <div v-if="imageLoading" class="loading-overlay">
            <div class="loading-spinner"></div>
            <p>加载中...</p>
          </div>
        </div>
      </div>
      
      <!-- 控制按钮 -->
      <div class="ppt-controls">
        <button 
          class="nav-btn prev-btn" 
          @click="previousPage" 
          :disabled="currentPage <= 1"
        >
          <i class="fas fa-chevron-left"></i>
          上一页
        </button>
        
        <div class="page-input-group">
          <input 
            type="number" 
            v-model.number="pageInput" 
            :min="1" 
            :max="totalPages"
            @keyup.enter="goToPage"
            class="page-input"
          />
          <button @click="goToPage" class="go-btn">跳转</button>
        </div>
        
        <button 
          class="nav-btn next-btn" 
          @click="nextPage" 
          :disabled="currentPage >= totalPages"
        >
          下一页
          <i class="fas fa-chevron-right"></i>
        </button>
      </div>
      
      <!-- 缩略图导航 -->
      <div class="thumbnail-nav" v-if="showThumbnails">
        <div class="thumbnail-container">
          <div 
            v-for="page in totalPages" 
            :key="page"
            class="thumbnail-item"
            :class="{ active: page === currentPage }"
            @click="goToSpecificPage(page)"
          >
            <img 
              :src="getThumbnailSrc(page)" 
              :alt="`第${page}页缩略图`"
              class="thumbnail-image"
            />
            <span class="thumbnail-number">{{ page }}</span>
          </div>
        </div>
      </div>
      
      <!-- 底部工具栏 -->
      <div class="ppt-toolbar">
        <button @click="toggleThumbnails" class="tool-btn">
          <i class="fas fa-th-large"></i>
          {{ showThumbnails ? '隐藏' : '显示' }}缩略图
        </button>
        <button @click="toggleFullscreen" class="tool-btn">
          <i class="fas fa-expand"></i>
          全屏
        </button>
        <button @click="downloadPPT" class="tool-btn download-ppt-btn">
          <i class="fas fa-file-powerpoint"></i>
          下载PPT
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PPTViewer3D',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    courseTitle: {
      type: String,
      default: '课程PPT'
    },
    pptImages: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      currentPage: 1,
      totalPages: 15, // 固定15张图片
      pageInput: 1,
      showThumbnails: false,
      imageLoading: false,
      pptBasePath: './static/ppt/'
    }
  },
  computed: {
    currentImageSrc() {
      return `${this.pptBasePath}ppt_test_${String(this.currentPage).padStart(2, '0')}.jpg`
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.currentPage = 1
        this.pageInput = 1
        this.imageLoading = true
        // 添加键盘事件监听
        document.addEventListener('keydown', this.handleKeydown)
      } else {
        // 移除键盘事件监听
        document.removeEventListener('keydown', this.handleKeydown)
      }
    },
    currentPage(newVal) {
      this.pageInput = newVal
    }
  },
  methods: {
    closePPT() {
      this.$emit('close')
    },
    
    handleOverlayClick() {
      this.closePPT()
    },
    
    previousPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.imageLoading = true
      }
    },
    
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.imageLoading = true
      }
    },
    
    goToPage() {
      const page = parseInt(this.pageInput)
      if (page >= 1 && page <= this.totalPages) {
        this.currentPage = page
        this.imageLoading = true
      } else {
        this.pageInput = this.currentPage
      }
    },
    
    goToSpecificPage(page) {
      this.currentPage = page
      this.imageLoading = true
    },
    
    getThumbnailSrc(page) {
      return `${this.pptBasePath}ppt_test_${String(page).padStart(2, '0')}.jpg`
    },
    
    toggleThumbnails() {
      this.showThumbnails = !this.showThumbnails
    },
    
    toggleFullscreen() {
      const element = this.$el
      if (!document.fullscreenElement) {
        element.requestFullscreen().catch(err => {
          console.log('无法进入全屏模式:', err)
        })
      } else {
        document.exitFullscreen()
      }
    },
    
    downloadPPT() {
      try {
        // 下载完整的PPT文件
        const link = document.createElement('a')
        link.href = './static/ppt_test.pptx'
        link.download = 'ppt_test.pptx'
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        
        // 显示下载提示
        console.log('PPT文件下载已开始...')
        
        // 可以添加一个简单的提示（可选）
        // alert('PPT文件下载已开始，请查看浏览器下载文件夹')
      } catch (error) {
        console.error('PPT文件下载失败:', error)
        alert('PPT文件下载失败，请稍后重试')
      }
    },
    
    handleKeydown(event) {
      switch(event.key) {
        case 'ArrowLeft':
          event.preventDefault()
          this.previousPage()
          break
        case 'ArrowRight':
          event.preventDefault()
          this.nextPage()
          break
        case 'Escape':
          event.preventDefault()
          this.closePPT()
          break
        case 'Home':
          event.preventDefault()
          this.currentPage = 1
          this.imageLoading = true
          break
        case 'End':
          event.preventDefault()
          this.currentPage = this.totalPages
          this.imageLoading = true
          break
      }
    },
    
    onImageLoad() {
      this.imageLoading = false
    },
    
    onImageError() {
      this.imageLoading = false
      console.error('PPT图片加载失败:', this.currentImageSrc)
    }
  },
  
  beforeDestroy() {
    document.removeEventListener('keydown', this.handleKeydown)
  }
}
</script>

<style scoped>
.ppt-viewer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  backdrop-filter: blur(5px);
}

.ppt-viewer-container {
  width: 90%;
  height: 90%;
  max-width: 1400px;
  max-height: 900px;
  background: white;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  position: relative;
  box-shadow: 
    0 20px 60px rgba(0, 0, 0, 0.15),
    0 8px 25px rgba(0, 0, 0, 0.1),
    0 4px 10px rgba(0, 0, 0, 0.05);
  /* 添加边框增强层次感 */
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.close-btn {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 40px;
  height: 40px;
  border: none;
  background: #ff4757;
  color: white;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  z-index: 10;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #ff3742;
  transform: scale(1.1);
}

.ppt-header {
  padding: 25px 40px;
  border-bottom: 2px solid #f1f2f6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px 12px 0 0;
  flex-shrink: 0; /* 防止标题栏被压缩 */
}

.ppt-header h3 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.page-indicator {
  font-size: 18px;
  font-weight: 500;
  background: rgba(255, 255, 255, 0.2);
  padding: 8px 16px;
  border-radius: 20px;
}

.ppt-content {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30px;
  background: #f8f9fa;
  min-height: 0; /* 确保flex子项可以收缩 */
}

.ppt-image-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /* 16:9 比例容器 */
  aspect-ratio: 16 / 9;
  max-width: 100%;
  max-height: 100%;
  /* 确保容器在flex布局中正确计算尺寸 */
  flex-shrink: 1;
  /* 添加边框和阴影增强视觉效果 */
  border: 1px solid #e1e8ed;
  border-radius: 10px;
  overflow: hidden;
  background: #ffffff;
  box-shadow: 
    0 8px 25px rgba(0, 0, 0, 0.08),
    0 4px 10px rgba(0, 0, 0, 0.05);
}

.ppt-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 0; /* 由容器提供圆角 */
  transition: all 0.3s ease;
  background: white;
  /* 确保图片完全填充容器 */
  display: block;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 8px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.ppt-controls {
  padding: 25px 40px;
  border-top: 2px solid #f1f2f6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  flex-shrink: 0; /* 防止控制栏被压缩 */
}

.nav-btn {
  padding: 12px 24px;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.prev-btn {
  background: linear-gradient(135deg, #74b9ff, #0984e3);
  color: white;
}

.next-btn {
  background: linear-gradient(135deg, #00b894, #00a085);
  color: white;
}

.nav-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.nav-btn:disabled {
  background: #ddd;
  color: #999;
  cursor: not-allowed;
  transform: none;
}

.page-input-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-input {
  width: 80px;
  padding: 10px;
  border: 2px solid #ddd;
  border-radius: 8px;
  text-align: center;
  font-size: 16px;
}

.go-btn {
  padding: 10px 20px;
  border: none;
  background: #6c5ce7;
  color: white;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.go-btn:hover {
  background: #5f3dc4;
}

.thumbnail-nav {
  max-height: 140px;
  overflow-y: auto;
  border-top: 2px solid #f1f2f6;
  background: #f8f9fa;
  padding: 20px;
  flex-shrink: 0; /* 防止缩略图区域被压缩 */
}

.thumbnail-container {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 10px;
}

.thumbnail-item {
  flex-shrink: 0;
  width: 90px;
  cursor: pointer;
  text-align: center;
  transition: all 0.3s ease;
}

.thumbnail-item:hover {
  transform: scale(1.05);
}

.thumbnail-item.active {
  transform: scale(1.1);
}

.thumbnail-image {
  width: 90px;
  height: 51px; /* 90 * 9/16 = 50.625，约51px保持16:9比例 */
  object-fit: cover;
  border-radius: 6px;
  border: 2px solid transparent;
  transition: all 0.3s ease;
  background: white;
}

.thumbnail-item.active .thumbnail-image {
  border-color: #667eea;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.thumbnail-number {
  display: block;
  font-size: 12px;
  margin-top: 4px;
  color: #666;
  font-weight: 500;
}

.thumbnail-item.active .thumbnail-number {
  color: #667eea;
  font-weight: 600;
}

.ppt-toolbar {
  padding: 20px 40px;
  border-top: 2px solid #f1f2f6;
  display: flex;
  justify-content: center;
  gap: 20px;
  background: white;
  border-radius: 0 0 12px 12px;
  flex-shrink: 0; /* 防止工具栏被压缩 */
}

.tool-btn {
  padding: 10px 20px;
  border: 2px solid #ddd;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.tool-btn:hover {
  border-color: #667eea;
  color: #667eea;
  transform: translateY(-1px);
}

.download-ppt-btn {
  background: linear-gradient(135deg, #e74c3c, #c0392b) !important;
  color: white !important;
  border-color: #c0392b !important;
}

.download-ppt-btn:hover {
  background: linear-gradient(135deg, #c0392b, #a93226) !important;
  border-color: #a93226 !important;
  color: white !important;
  transform: translateY(-1px) scale(1.02) !important;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .ppt-viewer-container {
    width: 95%;
    height: 95%;
  }
  
  .ppt-content {
    padding: 25px;
  }
  
  .ppt-header {
    padding: 20px 30px;
  }
  
  .ppt-controls {
    padding: 20px 30px;
  }
  
  .ppt-toolbar {
    padding: 15px 30px;
  }
}

@media (max-width: 768px) {
  .ppt-viewer-container {
    width: 98%;
    height: 98%;
    max-width: none;
    max-height: none;
  }
  
  .ppt-content {
    padding: 20px;
  }
  
  .ppt-header {
    padding: 15px 20px;
  }
  
  .ppt-header h3 {
    font-size: 18px;
  }
  
  .page-indicator {
    font-size: 14px;
    padding: 6px 12px;
  }
  
  .ppt-controls {
    padding: 15px 20px;
    flex-wrap: wrap;
    gap: 10px;
  }
  
  .nav-btn {
    padding: 10px 16px;
    font-size: 14px;
  }
  
  .page-input {
    width: 60px;
    padding: 8px;
  }
  
  .ppt-toolbar {
    padding: 10px 20px;
    flex-wrap: wrap;
    gap: 15px;
  }
  
  .tool-btn {
    padding: 8px 16px;
    font-size: 12px;
  }
  
  .thumbnail-item {
    width: 72px;
  }
  
  .thumbnail-image {
    width: 72px;
    height: 41px; /* 72 * 9/16 = 40.5，约41px */
  }
}

@media (max-width: 480px) {
  .ppt-viewer-container {
    width: 100%;
    height: 100%;
    border-radius: 0;
  }
  
  .ppt-content {
    padding: 15px;
  }
  
  .ppt-header {
    padding: 12px 15px;
    border-radius: 0;
  }
  
  .ppt-header h3 {
    font-size: 16px;
  }
  
  .page-indicator {
    font-size: 12px;
    padding: 4px 8px;
  }
  
  .ppt-controls {
    padding: 12px 15px;
    flex-direction: column;
    gap: 15px;
  }
  
  .page-input-group {
    order: -1;
  }
  
  .ppt-toolbar {
    padding: 8px 15px;
    gap: 10px;
  }
  
  .tool-btn {
    padding: 6px 12px;
    font-size: 11px;
  }
  
  .thumbnail-nav {
    max-height: 100px;
    padding: 15px;
  }
  
  .thumbnail-item {
    width: 60px;
  }
  
  .thumbnail-image {
    width: 60px;
    height: 34px; /* 60 * 9/16 = 33.75，约34px */
  }
}
</style> 